package com.jwater.launch;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import jwater.org.springframework.util.StringUtils;

/**
 * @description: 路径获取
 * @author: jnner
 * @createDate: 2021/2/5
 * @version: 1.0
 */
public class HomePath {
    private static final String HOME_PATH_NAME = "JWATER_HOME";

    private static String homePath = "";

    static {
        homePath = System.getenv().get(HOME_PATH_NAME);
        if (homePath == null) {
            homePath = System.getProperty(HOME_PATH_NAME);
        }
        homePath = homePath == null ? "" : homePath;
        homePath = homePath.replace("\\", "/");
    }

    /**
     * 获取远程主机的homePath
     *
     * @param remoteHomePath
     * @return
     */
    public static String getRemoteHomePath(String remoteHomePath) {
        if (!StringUtils.isEmpty(remoteHomePath)) {
            return remoteHomePath;
        }
        return getHomePath();
    }

    /**
     * 获取远程主机指定的path路径
     *
     * @param path
     * @param remoteHomePath
     * @return
     */
    public static String getRemotePath(String path, String remoteHomePath) {
        if (!StringUtils.isEmpty(remoteHomePath)) {
            path = path.replace(getHomePath(), remoteHomePath);
        }
        return path;
    }

    /**
     * 获取远程主机的临时安装包存放路径
     *
     * @param home
     * @param regionName
     * @param fileName
     * @return
     */
    public static String getRemoteInstallFilePath(String home, String regionName, String fileName) {
        return home + "/work/install/" + regionName + "/" + fileName;
    }

    /**
     * 获取远程主机的bin下的命令路径
     *
     * @param remoteHomePath
     * @param sysType
     * @return
     */
    public static String getRemoteBinFile(String remoteHomePath, String sysType) {
        if ("linux".equals(sysType)) {
            return getRemoteHomePath(remoteHomePath) + "/bin/jwater.sh";
        } else if ("windows".equals(sysType)) {
            return getRemoteHomePath(remoteHomePath) + "/bin/jwater.bat";
        }
        return "";
    }

    /**
     * 获取homePath路径
     *
     * @return
     */
    public static String getHomePath() {
        return homePath;
    }

    /**
     * 获取launch路径
     *
     * @return
     */
    public static String getLaunch() {
        return getHomePath() + "/launch/";
    }

    /**
     * 获取log路径
     *
     * @return
     */
    public static String getLog() {
        return getHomePath() + "/logs/";
    }

    /**
     * 获取region路径
     *
     * @return
     */
    public static String getRegion() {
        return getHomePath() + "/regions/";
    }

    /**
     * 获取regionDeploy路径
     *
     * @param regionName
     * @return
     */
    public static String getRegionDeploy(String regionName) {
        return getHomePath() + "/regions/" + regionName + "/";
    }

    /**
     * 获取conf路径
     *
     * @return
     */
    public static String getConfPath() {
        return getHomePath() + "/conf/";
    }

    /**
     * 获取data目录路径
     *
     * @return
     */
    public static String getDataPath() {
        return getHomePath() + "/data/";
    }

    /**
     * 获取menu所在路径
     *
     * @return
     */
    public static String getDataMenuPath() {
        return getHomePath() + "/data/menu/";
    }

    /**
     * 获取持久化存储路径
     *
     * @return
     */
    public static String getDurableDataPath() {
        return getHomePath() + "/work/data/";
    }

    /**
     * 获取临时文件路径
     *
     * @return
     */
    public static String getTmpFilePath() {
        return getHomePath() + "/work/tmp/";
    }

    /**
     * 获取临时安装包目录路径
     *
     * @param regionName
     * @param deployName
     * @return
     */
    public static String getTmpDeployPath(String regionName, String deployName) {
        return getHomePath() + "/work/deploy/" + regionName + "/" + deployName;
    }

    /**
     * 获取所有的region列表
     *
     * @return
     */
    public static List<String> getRegionNames() {
        List<String> list = new ArrayList<String>();
        File[] regionDirs = new File(getRegion()).listFiles();
        if (regionDirs == null) {
            return new ArrayList<>();
        }
        for (File regionDir : regionDirs) {
            list.add(regionDir.getName());
        }
        return list;
    }

    /**
     * 获取所有的deploy列表
     *
     * @param regionName
     * @return
     */
    public static List<String> getDeployNames(String regionName) {
        List<String> list = new ArrayList<String>();
        File[] deployDirs = new File(getRegionDeploy(regionName)).listFiles();
        if (deployDirs == null) {
            return new ArrayList<>();
        }
        for (File deployDir : deployDirs) {
            list.add(deployDir.getName());
        }
        return list;
    }

    /**
     * 获取jwater.core.xml匹配规则路径
     *
     * @return
     */
    public static String getCorePath() {
        return "file:" + getHomePath() + "/conf/jwater*.core.xml";
    }

    /**
     * 获取jwater.worker.xml匹配规则路径
     *
     * @return
     */
    public static String getWorkerXmlPath(String path) {
        return "file:" + path + "/conf/jwater*.worker.xml";
    }

    /**
     * 获取jwater.job.xml匹配规则路径
     *
     * @return
     */
    public static String getJobXmlPath(String path) {
        return "file:" + path + "/conf/jwater*.job.xml";
    }

    /**
     * 获取jwater.info.xml匹配规则路径
     *
     * @return
     */
    public static String getWorkerInfoPath(String regionName, String deployName) {
        return "file:" + getRegionDeploy(regionName) + deployName + "/jwater*.info.xml";
    }
}
